﻿using PMS.SANY.Entity;
using PMS.SANY.IBusiness;
using PMS.SANY.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;
using System.Text;
using PMS.SANY.Util;

namespace PMS.SANY.Api.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class OperatorController : BaseController
    {
        /// <summary>
        /// 
        /// </summary>
        public IServiceProvider SvcProvider { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public ISys_UserBusiness Bus { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Config { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IOperator Operator { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="svcProvider"></param>
        public OperatorController(IServiceProvider svcProvider)
        {
            this.SvcProvider = svcProvider;
            this.Operator = this.SvcProvider.GetRequiredService<IOperator>();
            this.Bus = this.SvcProvider.GetRequiredService<ISys_UserBusiness>();
            this.Config = this.SvcProvider.GetRequiredService<IConfiguration>();
        }

        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<UserModel> GetAsync()
        {
            return this.Operator.GetCurUser();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="data">登录参数</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<AjaxResult<string>> LoginAsync(LoginQM data)
        {
            var user = await this.Bus.LoginAsync(data);
            var jwtOption = this.Config.GetSection("JwtAuth").Get<JwtOptions>();
            var claims = new[]
            {
                new System.Security.Claims.Claim("userId",user.Id)
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOption.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(
                string.Empty,
                string.Empty,
                claims,
                expires: DateTime.Now.AddHours(jwtOption.ExpireHours),
                signingCredentials: credentials);
            return Success(new JwtSecurityTokenHandler().WriteToken(jwtToken));
        }

        /// <summary>
        /// 用户登出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<UserModel> Logout()
        {
            return this.Operator.GetCurUser();
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<dynamic> GetUserInfo()
        {
            return this.Bus.GetUserInfo(this.Operator.UserId);
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<List<Sys_Menu>> GetUserMenu()
        {
            return this.Bus.GetUserMenu(this.Operator.UserId);
        }

        /// <summary>
        /// 测试提交
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> SubmitAsync(object data)
        {
            await Task.Delay(new Random(unchecked((int)DateTime.Now.Ticks)).Next(1000, 8000));
            return data;
        }
    }
}
